//
//  SPVenueViewController.m
//  Sport
//
//  Created by Cass on 2017/11/24.
//  Copyright © 2017年 Cass. All rights reserved.
//

#import "SPVenueViewController.h"
#import "SPSeekViewController.h"
#import "SPVenueInfoCell.h"
#import "SPStoreViewController.h"
#import "SPInfoModel.h"
#import <FTPopOverMenu.h>
@interface SPVenueViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UIImageView * _districtView;
    NSArray * _districtArray;
    
    UIImageView * _typeView;
    NSArray * _typeArray;
    
    UIImageView * _orderView;
    NSArray * _orderArray;
    
    UITableViewController *_districtVC;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *district;
@property (weak, nonatomic) IBOutlet UIButton *type;
@property (weak, nonatomic) IBOutlet UIButton *order;

@property (strong, nonatomic) NSMutableArray<SPInfoModel *> *states;

@end

@implementation SPVenueViewController


- (NSMutableArray<SPInfoModel *> *)states{
    if (!_states) {
        _states = [[NSMutableArray alloc]init];
    }
    return _states;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _districtArray = @[@"成都"];
    _typeArray = @[@"羽毛球"];
    _orderArray = @[@"按照评分",@"按照价格"];
    
    [self.district setTitle:@"地区" forState:(UIControlStateNormal)];
    self.district.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.district.titleLabel setAdjustsFontSizeToFitWidth:YES];

    [self.type setTitle:@"类型" forState:(UIControlStateNormal)];
    self.type.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.type.titleLabel setAdjustsFontSizeToFitWidth:YES];
    
    [self.order setTitle:@"排序" forState:UIControlStateNormal];
    self.order.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.order.titleLabel setAdjustsFontSizeToFitWidth:YES];
    
    
    [self loadNewData];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.navigationItem.title = @"场馆";
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:nil selectedImage:nil title:@"搜索" target:self action:@selector(seek:)];
}

- (void)seek:(UIBarButtonItem *)barButtonItem{
    SPSeekViewController *seekVC = [[UIStoryboard storyboardWithName:@"Venue" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier: NSStringFromClass([SPSeekViewController class])];
    [self.navigationController pushViewController:seekVC animated:YES];
}

//根据价格排序
- (void)varbArrayByPrice{
    NSMutableArray<SPInfoModel *> *Array = [NSMutableArray arrayWithArray:_states];//临时装入原数组
    NSMutableArray<SPInfoModel *> *tArray = [NSMutableArray array];//装入排序后数组
    NSNumber *price;
    int tIndex = 0;
    for (int index = 0; index < Array.count; index++) {
        for (int index = 0; index < Array.count; index++) {
            if (index == 0) {
                price = Array[0].venuesprice;
                tIndex = 0;
            }
            else if (price.integerValue <= Array[index].venuesprice.integerValue) {
                price = Array[index].venuesprice;
                tIndex = index;
            }
        }
        [tArray addObject:Array[tIndex]];
        [Array removeObjectAtIndex:tIndex];
        index--;
    }
    self.states = tArray;
}
//根据评分排序
- (void)varbArrayByMArk{
    NSMutableArray<SPInfoModel *> *Array = [NSMutableArray arrayWithArray:_states];//临时装入原数组
    NSMutableArray<SPInfoModel *> *tArray = [NSMutableArray array];//装入排序后数组
    NSNumber *mark;
    int tIndex = 0;
    for (int index = 0; index < Array.count; index++) {
        for (int index = 0; index < Array.count; index++) {
            if (index == 0) {
                mark = Array[0].venuesmark;
                tIndex = 0;
            }
            else if (mark.integerValue <= Array[index].venuesmark.integerValue) {
                mark = Array[index].venuesmark;
                tIndex = index;
            }
        }
        [tArray addObject:Array[tIndex]];
        [Array removeObjectAtIndex:tIndex];
        index--;
    }
    self.states = tArray;
    
}
- (IBAction)districtClick:(id)sender {
    [FTPopOverMenu showForSender:_district withMenuArray:_districtArray doneBlock:^(NSInteger selectedIndex) {
        switch (selectedIndex) {
            case 0:
                [_district setTitle:_districtArray[0] forState:UIControlStateNormal];
                break;
            default:
                break;
        }
    } dismissBlock:^{
        NSLog(@"取消选择");
    }];
}

- (IBAction)typeClick:(id)sender {
    [FTPopOverMenu showForSender:_type withMenuArray:_typeArray doneBlock:^(NSInteger selectedIndex) {
        switch (selectedIndex) {
            case 0:
                [_type setTitle:_typeArray[0] forState:UIControlStateNormal];
                break;
            default:
                break;
        };
    } dismissBlock:^{
        NSLog(@"取消选择");
    }];
}

- (IBAction)orderClick:(id)sender {
    [FTPopOverMenu showForSender:_order withMenuArray:_orderArray doneBlock:^(NSInteger selectedIndex) {
        switch (selectedIndex) {
            case 0:
                [_order setTitle:_orderArray[0] forState:UIControlStateNormal];
                [self varbArrayByMArk];
                [self.tableView reloadData];
                break;
            case 1:
                [_order setTitle:_orderArray[1] forState:UIControlStateNormal];
                [self varbArrayByPrice];
                [self.tableView reloadData];
                break;
            default:
                break;
        }
    } dismissBlock:^{
        NSLog(@"取消选择");
    }];
}

- (void)loadNewData{
    NSString *url = [NSString stringWithFormat:@"http://39.108.132.194/Sport/venuesinfo.php"];
    [SPHttpClient POST:url parameters:nil success:^(id responseObject) {
        NSArray *info = responseObject;
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dict in info) {
            SPInfoModel *state = [SPInfoModel mj_objectWithKeyValues:dict];
            [arr addObject:state];
        }
        self.states  = arr;
        [self varbArrayByMArk];
        [self varbArrayByPrice];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        nil;
    }];
}

#pragma mark - UITableViewDataSource&&UITableViewDelegate -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.states.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SPVenueInfoCell *cell = [SPVenueInfoCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.infoModel = self.states[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 14.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.0f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SPStoreViewController *storeVC = [[UIStoryboard storyboardWithName:@"Venue" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPStoreViewController class])];
    storeVC.infoModel = self.states[indexPath.row];
    [self.navigationController pushViewController:storeVC animated:YES];
    
}


@end
